import { asyncRoutes, constantRoutes } from '@/router'
const state = {
  routes: constantRoutes// 默认静态路由
}
const mutations = {
  // otherRoutes登录成功后，需要添加的新路由
  setRoutes(state, otherRoutes) {
    // 静态路由基础上，累加其他权限路由
    state.routes = [...constantRoutes, ...otherRoutes]
  }
}
const actions = {
  // 筛选路由权限
  filterRoutes(context, menus) {
    // asyncRouter是所有的路由
    const otherRoutes = asyncRoutes.filter(item => {
      if (menus.includes(item.children[0].name)) {
        return true
      } else {
        return false
      }
    })
    context.commit('setRoutes', otherRoutes)
    return otherRoutes
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
